home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JMEMANSI.C < prev    next >
C/C++ Source or Header  |  1992-03-13  |  4KB  |  158 lines

  1. /*
  2.  * jmemansi.c  (jmemsys.c)
  3.  *
  4.  * Copyright (C) 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file provides a simple generic implementation of the system-
  9.  * dependent portion of the JPEG memory manager.  This implementation
  10.  * assumes that you have the ANSI-standard library routine tmpfile().
  11.  * Also, the problem of determining the amount of memory available
  12.  * is shoved onto the user.
  13.  */
  14.  
  15. #include "jinclude.h"
  16. #include "jmemsys.h"
  17.  
  18. #ifdef INCLUDES_ARE_ANSI
  19. #include <stdlib.h>        /* to declare malloc(), free() */
  20. #else
  21. extern void * malloc PP((size_t size));
  22. extern void free PP((void *ptr));
  23. #endif
  24.  
  25. #ifndef SEEK_SET        /* pre-ANSI systems may not define this; */
  26. #define SEEK_SET  0        /* if not, assume 0 is correct */
  27. #endif
  28.  
  29.  
  30. static external_methods_ptr methods; /* saved for access to error_exit */
  31.  
  32. static long total_used;        /* total memory requested so far */
  33.  
  34.  
  35. /*
  36.  * Memory allocation and freeing are controlled by the regular library
  37.  * routines malloc() and free().
  38.  */
  39.  
  40. GLOBAL void *
  41. jget_small (size_t sizeofobject)
  42. {
  43.   total_used += sizeofobject;
  44.   return (void *) malloc(sizeofobject);
  45. }
  46.  
  47. GLOBAL void
  48. jfree_small (void * object)
  49. {
  50.   free(object);
  51. }
  52.  
  53. /*
  54.  * We assume NEED_FAR_POINTERS is not defined and so the separate entry points
  55.  * jget_large, jfree_large are not needed.
  56.  */
  57.  
  58.  
  59. /*
  60.  * This routine computes the total memory space available for allocation.
  61.  * It's impossible to do this in a portable way; our current solution is
  62.  * to make the user tell us (with a default value set at compile time).
  63.  * If you can actually get the available space, it's a good idea to subtract
  64.  * a slop factor of 5% or so.
  65.  */
  66.  
  67. #ifndef DEFAULT_MAX_MEM        /* so can override from makefile */
  68. #define DEFAULT_MAX_MEM        1000000L /* default: one megabyte */
  69. #endif
  70.  
  71. GLOBAL long
  72. jmem_available (long min_bytes_needed, long max_bytes_needed)
  73. {
  74.   return methods->max_memory_to_use - total_used;
  75. }
  76.  
  77.  
  78. /*
  79.  * Backing store (temporary file) management.
  80.  * Backing store objects are only used when the value returned by
  81.  * jmem_available is less than the total space needed.  You can dispense
  82.  * with these routines if you have plenty of virtual memory; see jmemnobs.c.
  83.  */
  84.  
  85.  
  86. METHODDEF void
  87. read_backing_store (backing_store_ptr info, void FAR * buffer_address,
  88.             long file_offset, long byte_count)
  89. {
  90.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  91.     ERREXIT(methods, "fseek failed on temporary file");
  92.   if (JFREAD(info->temp_file, buffer_address, byte_count)
  93.       != (size_t) byte_count)
  94.     ERREXIT(methods, "fread failed on temporary file");
  95. }
  96.  
  97.  
  98. METHODDEF void
  99. write_backing_store (backing_store_ptr info, void FAR * buffer_address,
  100.              long file_offset, long byte_count)
  101. {
  102.   if (fseek(info->temp_file, file_offset, SEEK_SET))
  103.     ERREXIT(methods, "fseek failed on temporary file");
  104.   if (JFWRITE(info->temp_file, buffer_address, byte_count)
  105.       != (size_t) byte_count)
  106.     ERREXIT(methods, "fwrite failed on temporary file --- out of disk space?");
  107. }
  108.  
  109.  
  110. METHODDEF void
  111. close_backing_store (backing_store_ptr info)
  112. {
  113.   fclose(info->temp_file);
  114.   /* Since this implementation uses tmpfile() to create the file,
  115.    * no explicit file deletion is needed.
  116.    */
  117. }
  118.  
  119.  
  120. /*
  121.  * Initial opening of a backing-store object.
  122.  *
  123.  * This version uses tmpfile(), which constructs a suitable file name
  124.  * behind the scenes.  We don't have to use temp_name[] at all;
  125.  * indeed, we can't even find out the actual name of the temp file.
  126.  */
  127.  
  128. GLOBAL void
  129. jopen_backing_store (backing_store_ptr info, long total_bytes_needed)
  130. {
  131.   if ((info->temp_file = tmpfile()) == NULL)
  132.     ERREXIT(methods, "Failed to create temporary file");
  133.   info->read_backing_store = read_backing_store;
  134.   info->write_backing_store = write_backing_store;
  135.   info->close_backing_store = close_backing_store;
  136. }
  137.  
  138.  
  139. /*
  140.  * These routines take care of any system-dependent initialization and
  141.  * cleanup required.  Keep in mind that jmem_term may be called more than
  142.  * once.
  143.  */
  144.  
  145. GLOBAL void
  146. jmem_init (external_methods_ptr emethods)
  147. {
  148.   methods = emethods;        /* save struct addr for error exit access */
  149.   emethods->max_memory_to_use = DEFAULT_MAX_MEM;
  150.   total_used = 0;
  151. }
  152.  
  153. GLOBAL void
  154. jmem_term (void)
  155. {
  156.   /* no work */
  157. }
  158.